package com.example.demo.dto;

import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.regex.Pattern;

import javax.print.DocFlavor.STRING;
import javax.servlet.http.HttpServletRequest;

public class SearchInfo {

	private long page;
	private long limit;
	
	
	public void initSearch(ResultInfo info,HttpServletRequest req) {
		Enumeration<String> en=req.getParameterNames();
		StringBuilder where=new StringBuilder(" where ");
		while(en.hasMoreElements()) {
			String p=en.nextElement();
			String np=p;
			Field f=null;
			String ex="=";
			if(p.equals("page")||p.equals("limit"))continue;
			if(p.indexOf("l_")==0) {
				ex="like";
				np=p.substring(2);
			}
			String PP="^(\\-|\\+)?\\d+(\\.\\d+)?$";
			//设置参数
			Object ov=req.getParameter(p);
			Object v=ov;
			if(ex.equals("like")) v="'%"+v+"%'";
			else if(! Pattern.matches(PP,(CharSequence) v)) v="'"+v+"'";
			 where.append(np+" "+ex+" "+v +" and ");
			 info.addParam(p, ov);
		}
		if(where.length()<9) where=new StringBuilder();
		else where.delete(where.length()-4, where.length());
		this.setWhere(where.toString());
	}
	

	public void setPage(long page) {
		this.page = page;
	}
	
	public void setLimit(long limit) {
		this.limit = limit;
	}

	private String where;
	private String limitstr;
	public String getWhere() {
		return where;
	}
	public void setWhere(String where) {
		this.where = where;
	}
	public String getLimitstr() {
		return page>0?(" limit "+(page-1)*limit+","+limit):"";
	}

	public void setLimitstr(String limitstr) {
		this.limitstr = limitstr;
	}
	
	
	
}
